<template>
  <div>
    <div class="search" style="display: flex">
      <el-input size="medium" placeholder="输入最低规格价格筛选" style="width: 200px;margin-left: 10px" v-model="minPrice"></el-input>
      <el-input size="medium" placeholder="输入最高规格价格筛选" style="width: 200px;margin-left: 10px" v-model="maxPrice"></el-input>
      <el-button size="medium" type="info"  style="margin-left: 20px" @click="load(1)">查询</el-button>
      <el-button size="medium" type="warning"  style="margin-left: 20px" @click="reset">重置</el-button>
    </div>
<!--    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </div>-->

    <div class="table">
      <el-table :data="tableData" strip @selection-change="handleSelectionChange" style="width: 100%;">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" min-width="80" align="center">
          <template v-slot="scope">{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="medicinesName" label="药物名称" min-width="150" align="center"></el-table-column>
        <el-table-column prop="typeName" label="药物分类名称" min-width="150" align="center"></el-table-column>
        <el-table-column prop="dosageForm" label="药物剂型" min-width="150" align="center"></el-table-column>
        <el-table-column prop="price" label="规格价格" min-width="150"  align="center"></el-table-column>
        <el-table-column prop="stylePhoto" label="样式图" align="center" width="100">
          <template v-slot="scope">
            <el-image v-if="scope.row.stylePhoto" style="width: 60px" :src="scope.row.stylePhoto" :preview-src-list="[scope.row.stylePhoto]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="styleDescribe" label="规格描述" min-width="200" align="center"></el-table-column>
        <el-table-column label="操作" align="center" min-width="180">
          <template v-slot="scope">
            <el-button size="medium" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <div style="display: flex;margin-top: 20px">
        <el-select
            size="small"
            v-model="operateType" placeholder="批量操作">
          <el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
        <el-button
            style="margin-left: 20px"
            class="search-button"
            @click="handleBatchOperate()"
            type="primary"
            size="small">
          确定
        </el-button>
        <div style="margin-left: auto;">
          <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handlePageChange"
              layout="total, sizes, prev, pager, next, jumper"
              :page-size="pageSize"
              :page-sizes="[5, 10, 15]"
              :current-page.sync="pageNum"
              :total="total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog title="药品规格表" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
        <el-form-item label="药品ID" prop="medicinesId">
          <el-input v-model="form.medicinesId" placeholder="药品ID"></el-input>
        </el-form-item>
        <el-form-item label="规格价格" prop="price">
          <el-input v-model="form.price" placeholder="规格价格"></el-input>
        </el-form-item>
        <el-form-item label="规格描述" prop="styleDescribe">
          <el-input v-model="form.styleDescribe" placeholder="规格描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>
export default {
  name: "MedicinesStyle",
  data() {
    let MedicinesId = this.$route.query.medicinesId
    return {
      operates: [
        {label: "批量删除", value: "delete"},
      ],
      minPrice:null,
      maxPrice:null,
      operateType: null,
      medicinesId:MedicinesId,
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      fromVisible: false,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
      },
      ids: []
    }
  },
  created() {
    this.load(1)
  },
  methods: {
    handleBatchOperate() {
      if(this.operateType==null){
        this.$message({
          message: '请选择操作类型',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      if(!this.ids.length){
        this.$message({
          message: '请选择要操作的用户',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      this.$confirm('是否要进行该批量操作?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids=this.ids;
        switch (this.operateType) {
          case this.operates[0].value:
            this.delBatch(ids)
            break;
        }
      }).catch(() => {
        // 取消按钮被点击时的操作
        console.log('用户取消了操作');
      });
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/medicinesStyle/update' : '/medicinesStyle/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/medicinesStyle/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/medicinesStyle/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {
      if (pageNum) this.pageNum = pageNum;
      this.$request.get('/medicinesStyle/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          medicinesId:this.medicinesId,
          minPrice:this.minPrice,
          maxPrice:this.maxPrice,
        }
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data?.list || [];
          this.total = res.data?.total || 0;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    reset() {
      this.minPrice=null;
      this.maxPrice=null;
      this.load(1);
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.load(1); // Reset to the first page when changing page size
    },
    handlePageChange(newPage) {
      this.load(newPage);
    }
  }
}
</script>

<style scoped>

</style>
